home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 468 < prev    next >
Text File  |  1996-08-06  |  6KB  |  125 lines

  1. Path: mail2news.demon.co.uk!pires.demon.co.uk
  2. From: rpp <rpp@pires.co.uk>
  3. Newsgroups: comp.std.c
  4. Subject: Re: Is a diagnostic required?
  5. Date: Wed, 28 Feb 1996 16:19:39 GMT
  6. Organization: Pi Technology Ltd
  7. Distribution: world
  8. Message-ID: <218226403wnr@pires.co.uk>
  9. References: <1996Feb23.090526.7591@sq.com> <danpop.824999740@rscernix>
  10. Reply-To: rpp@pires.co.uk
  11. X-NNTP-Posting-Host: pires.demon.co.uk
  12. X-Broken-Date: Wednesday, Feb 28, 1996 16.19.39 GMT
  13. X-Newsreader: Newswin Alpha 0.6
  14. X-Mail2News-Path: disperse.demon.co.uk!post.demon.co.uk!pires.demon.co.uk
  15.  
  16. In article: <1996Feb23.090526.7591@sq.com>  msb@sq.com (Mark Brader) writes:
  17. > Dan Pop (danpop@mail.cern.ch) writes:
  18. > > Does the following code require a diagnostic?
  19. > > 
  20. > >     foo() { }
  21. > >     main() { foo(3); } > 
  22. > > I was always convinced that there is no difference between foo() and
  23. > > foo(void) in the _definition_ of the function foo ... Yet, all the
  24. > > compilers I tried accepted this code without complaint and complained
  25. > > when 'void' was introduced in the definition of foo.
  26. > The language of the standard is fuzzy here, but I believe that it is
  27. > clearly intended for the diagnostic to be mandatory when the "void" is
  28. > present, and optional otherwise (the behavior being undefined).
  29. > An excerpt from the Constraints of section 6.3.2.2/3.3.2.2 (emphasis added):
  30. > #  If the expression that denotes the called function has a type that
  31. > #  INCLUDES a prototype, the number of arguments shall agree with the
  32. > #  number of parameters.
  33. > As this is a Constraint, a violation requires a diagnostic.  But from the
  34. > Semantics of the same section (emphasis added):
  35. > #  If the expression that denotes the called function has a type that
  36. > #  DOES NOT include a prototype ...  If the number of arguments does
  37. > #  not agree with the number of parameters, the behavior is undefined.
  38. > Thus no diagnostic is required if this is the only violation.  I must
  39. > say it's rather disgusting that none of the compilers Dan tried was smart
  40. > and helpful enough to produce the diagnostic anyway -- it would after
  41. > all be easy enough for them to do so in this case.  (Cancel that remark
  42. > if they were all compilers intended to be used with lint for checking.)
  43. Don't be too unfair to the compiler writers - they're trying to write
  44. compilers that accept K&R C as well as ISO C. You may not think this is a
  45. good idea, but there's vast amounts of legacy code with K&R header 
  46. files out there.... The authors of the standard tried to make it 
  47. compatible with "current practice" (ie K&R) as far as possible: that is 
  48. why the declaration of foo() with no parameters is allowed at all.
  49. > Now, "prototype" is defined in 6.1.2.1/3.1.2.1:
  50. > #  A "function prototype" is a declaration of a function that declares
  51. > #  the types of its parameters.
  52. > And in case there was any doubt, both 6.5/3.5 and 6.7.1/3.7.1 make it
  53. > clear that a definition is a kind of declaration.  I won't bother to
  54. > quote them here.
  55. > These passages are sloppy for the following reasons:
  56. > 1. "Function prototype" is defined, but the term used elsewhere in the
  57. >    standard is just "prototype".
  58. > 2. It is defined as a syntactic construct, not something that might be
  59. >    "included" in a type.
  60. > 3. If a function has no parameters, it is not possible to distinguish
  61. >    whether they are declared or not.
  62. > 4. The old-style function definition
  63. >     void fu (x,y) short x,y; { }
  64. >    is a declaration that declares the types of the function's parameters,
  65. >    but if this was deemed to be a prototype, existing code would be broken.
  66. >    The way to resolve both points 3 and 4 is to relate the term "prototype"
  67. >    more directly to the new-style syntax, which it is obviously intended
  68. >    to denote.  In section 6.5.4.3/3.5.4.3, where function declarators and
  69. >    their semantics are actually defined, the term is avoided altogether and
  70. >    "parameter type list" is used instead.  "Prototype" probably should have
  71. >    been defined in these terms.  Note that void is a type, so foo(void)
  72. >    would be a valid function declarator with a prototype, while foo()
  73. >    would unambiguously not have a prototype.  There is no syntactic
  74. >    ambiguity since a parameter type list is not allowed to be empty.
  75. > 5. The punctuation of the quoted passage from 6.3.2.2/3.3.2.2 Semantics
  76. >    is ambiguous: it isn't clear whether the first If-clause quoted is
  77. >    meant to govern the second one, which is in a different sentence.
  78. >    More precisely, the punctuation suggests that it does not, but in
  79. >    other places in the standard similarly punctuated, it would.  In
  80. >    this case, however, material later in the paragraph suggests that
  81. >    the last-quoted If-clause is not meant to be within the scope of the
  82. >    other one.
  83. >    Fortunately, this parsing ambiguity makes no difference in this case;
  84. >    the overall effect is the same with either parsing, no matter whether
  85. >    a prototype is used or not.
  86. > If you take the intended readings as being what I said, then you end up
  87. > with the conclusion I gave above.  If not, you probably don't.
  88. > -- 
  89. > Mark Brader, msb@sq.com       "I'm not a lawyer, but I'm pedantic and
  90. > SoftQuad Inc., Toronto         that's just as good."      -- D Gary Grady
  91. > My text in this article is in the public domain.
  92.  
  93. --
  94. Richard P. Parkins, M. A.     rpp@pires.co.uk
  95. Pi Technology Ltd.            +44 (0)1223 441434
  96. Opinions expressed were mine at the time of posting,
  97. otherwise all possible disclaimers apply.
  98.  
  99.  
  100.